import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.JOptionPane;

import lejos.util.Delay;
import Messaging.Hit;
import Messaging.IMessage;
import Messaging.Miss;

public class HitControl extends Thread {
	ArrayBlockingQueue<Hit> queue;
	ArrayBlockingQueue<IMessage> predatorQueue;
	ArrayBlockingQueue<IMessage> preyQueue;

	public HitControl(ArrayBlockingQueue<Hit> queue,
			ArrayBlockingQueue<IMessage> predatorQueue,
			ArrayBlockingQueue<IMessage> preyQueue) {
		this.queue = queue;
		this.predatorQueue = predatorQueue;
		this.preyQueue = preyQueue;
	}

	@Override
	public void run() {

		long timePred = 0, timePrey = 0;

		while (true) {
			try {
				while(queue.size()>0)
				{
					Hit hit = queue.take();
					if (hit.GetSender().equals(RobotNames.Predator)) {
						timePred = System.currentTimeMillis();
						if (Math.abs((timePred - timePrey)) < 2000) {
							predatorQueue.add(new Hit());
							preyQueue.add(new Hit());
						} else {
							predatorQueue.add(new Miss());

						}
					} else if (hit.GetSender().equals(RobotNames.Prey)) {
						timePrey = System.currentTimeMillis();
					}
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			Delay.msDelay(70);
		}
	}

}
